package org.spamforces.configuration;

import com.codeforces.commons.properties.PropertiesUtil;
import com.codeforces.commons.text.StringUtil;
import org.apache.commons.lang.ArrayUtils;

import javax.annotation.Nullable;
import java.text.MessageFormat;
import java.util.TimeZone;

/**
 * @author Mike Mirzayanov (mirzayanovmr@gmail.com)
 * @author Maxim Shipko (sladethe@gmail.com)
 */
@SuppressWarnings("AccessOfSystemProperties")
public class ApplicationConfiguration {
    public static TimeZone getTimeZone() {
        return TimeZone.getTimeZone(getProperty("timezone", "Europe/Moscow"));
    }

    @Nullable
    public static String getSmsServiceUrl() {
        return MessageFormat.format(getProperty("sms-service.url-pattern", null), ArrayUtils.EMPTY_OBJECT_ARRAY);
    }

    @Nullable
    public static String getSmsServicePhoneParameterName() {
        return getSmsServiceParameterName("phone");
    }

    @Nullable
    public static String getSmsServiceMessageParameterName() {
        return getSmsServiceParameterName("message");
    }

    @Nullable
    public static String getSmsServiceParameterName(String applicationParameterName) {
        return StringUtil.trimToNull(getProperty("sms-service.parameter." + applicationParameterName, null));
    }

    private static String getProperty(String propertyName, String defaultValue) {
        return PropertiesUtil.getProperty(propertyName, defaultValue, "/spamforces.properties");
    }

    private ApplicationConfiguration() {
        throw new UnsupportedOperationException();
    }

    static {
        System.setProperty("file.encoding", "UTF-8");
    }
}
